package login;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by RaysMac on 14. 12. 12..
 */
@WebServlet(name = "Login")
public class Login extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        String uri=request.getRequestURI();
        if(uri.equals("/Login"))
        {
            String id= request.getParameter("id");
            String pw= request.getParameter("pw");
            if(id == null || pw==null || id.length() ==0 || pw.length()==0){
                response.sendError(HttpServletResponse.SC_BAD_REQUEST);
                return;
            }

            User user =Users.getUsers(id);
            if(user == null){
                response.getWriter().println("존재하지 않는 아이디 입니다.");

                return;
            }
            else if(!user.pw.equals(pw)){
                response.getWriter().println("비밀번호가 올바르지 않습니다.");
                return;
            }
            request.getSession().setAttribute("loginid",user.id);
        }
        else if(uri.equals("/Logout")){
            request.getSession().invalidate();
        }
        response.sendRedirect("/login.jsp");

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.sendRedirect("/login.jsp");
    }
}
